package com.common.base.utils

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：17/2/2023
 *
 * 描述：分页工具
 *
 * 修订历史：
 *
 */
class DividePageUtils(pageSize: Int, recordCount: Int, currentPage: Int) {
    private var pageSize // 每页显示的条数
            : Int
    private var recordCount // 记录的总条数
            : Int
    private var currentPage // 当前页
            = 0
    private var pageCount // 总页数
            = 0

    init {
        this.pageSize = pageSize
        this.recordCount = recordCount
        setCurrentPage(currentPage)
    }

    fun getPageSize(): Int {
        return pageSize
    }

    fun setPageSize(pageSize: Int) {
        this.pageSize = pageSize
    }

    fun getRecordCount(): Int {
        return recordCount
    }

    fun setRecordCount(recordCount: Int) {
        this.recordCount = recordCount
    }

    fun getCurrentPage(): Int {
        return currentPage
    }

    /**
     * 设置定位在当前页
     *
     * @param currentPage
     */
    fun setCurrentPage(currentPage: Int) {
        var activePage = if (currentPage <= 0) 1 else currentPage
        activePage = if (activePage > getPageCount()) getPageCount() else activePage
        this.currentPage = activePage
    }

    /**
     * 获得总页数
     *
     * @return
     */
    fun getPageCount(): Int {
        pageCount = recordCount / pageSize
        val mod = recordCount % pageSize
        if (mod != 0) {
            pageCount++
        }
        return if (recordCount == 0) 1 else pageCount
    }

    fun getFromIndex(): Int {
        return (currentPage - 1) * pageSize
    }

    fun getToIndex(): Int {
        return Math.min(recordCount, currentPage * pageSize)
    }

    fun setPageCount(pageCount: Int) {
        this.pageCount = pageCount
    }
}